import { _decorator, Animation, AudioClip, Button, Component, error, find, Label, Node, resources, Vec3 } from 'cc';
import { GLOBALVar } from './GlobalVar';
import { GLOBALUtil } from './GlobalUtil';
import { SoundManager } from './SoundManager';
const { ccclass, property } = _decorator;

@ccclass('ShopManager')
export class ShopManager extends Component {
    @property({type: Node, tooltip: "商店页面"})
    private ShopPanel: Node | null = null;
    @property({type: Label, tooltip: "货币数量"})
    private CoinNumLabel: Label | null = null;
    
    private MessageNode: Node | null = null;
    start() {

    }

    update(deltaTime: number) {
        
    }

    //商店
    onClickShopBtn() {
        this.ShopPanel.active = true;
        const StoreNode = this.ShopPanel.getChildByName('StoreNode');
        StoreNode.getComponent(Animation).play();
        //获取服务器上的金币
        GLOBALUtil.refreshCoins(() => {
            let Coins = GLOBALVar.getCoins();
            this.CoinNumLabel.string = Coins.toString() + "$";
        });
        this.updateShopShow();
    }
    
    onClickCloseShop() {
        if(this.MessageNode) {
            this.MessageNode.destroy();
        }
        this.ShopPanel.active = false;
    }

    onClickBuy(event: Event, p: string) {
        const soundManager = SoundManager.getInstance();
        resources.load('Audios/PVZ/coin', AudioClip, (err, clip) => {
            if (err) {
                console.error(err);
                return;
            }
            const soundManager = SoundManager.getInstance();
            soundManager.playSFX(clip);
        })
        //设置按钮点击间隔
        const btn = (event.target as any).getComponent(Button);
        btn.interactable = false;
        setTimeout(() => {
            btn.interactable = true;
        }, 1000);
        let node = find("CanvasMain");
        const itemId = parseInt(p);
        GLOBALUtil.requestBuyItem(itemId, 1)
        .then((response) => {
            this.CoinNumLabel.string = response.coins.toString() + "$";
            if(this.MessageNode) {
                this.MessageNode.destroy();
            }
            let msgType: number;
            if (response.msg == "金币不足！") {
                msgType = 2;
            } else {
                msgType = 0;
            }
            GLOBALUtil.LoadMessage(true, response.msg, node, new Vec3(50, 950, 0), false, msgType)
            .then((node: Node) => {
                this.MessageNode = node;
            })
            this.updateShopShow();
        })
    }

    //更新商店页面的显示
    updateShopShow() {
        GLOBALUtil.getCardFrame()
        .then((response: JSON) => {
            for (const key in response) {
                if (Object.prototype.hasOwnProperty.call(response, key)) {
                    const element = response[key];
                    if (element == 1) {
                        const cardFrameNode = this.ShopPanel.getChildByPath('StoreNode/ShopPageOne/CardFrames/CardFrame_' + key);
                        const BtnPurchase = cardFrameNode.getChildByName('BtnPurchase');
                        const Price = cardFrameNode.getChildByName('Price');
                        const Bought = cardFrameNode.getChildByName('Bought');
                        BtnPurchase.active = false;
                        Price.active = false;
                        Bought.active = true;
                    }
                }
            }
        }).catch(error => {
            console.error(error);
        })
    }
}


